package com.justgame.project.service.just.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.justgame.project.cloud.common.constant.Result;
import com.justgame.project.cloud.common.util.MapBuilder;
import com.justgame.project.service.just.entity.JustGameInfo;
import com.justgame.project.service.just.dto.JustGameInfoDTO;
import com.justgame.project.service.just.wrapper.JustGameInfoWrapper;
import com.justgame.project.service.just.controller.form.JustGameInfoQueryForm;
import com.justgame.project.service.just.service.IJustGameInfoService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.Assert;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 *
 * 游戏信息表控制器
 *
 * @@tableName just_game_info
 * @author Zwb
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/just/game/info")
@Api("游戏信息表控制器")
public class JustGameInfoController {

    private final IJustGameInfoService justGameInfoService;

    @GetMapping("/detail")
    @ApiOperation(value = "通过主键id查询 游戏信息表")
    public Result<?> query(@RequestParam("id") Long id){
        return Result.ok(justGameInfoService.getById(id));
    }

    @PutMapping("/update")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id编辑 游戏信息表")
    public Result<?> edit(@ApiParam("编辑表单") @RequestBody @Validated JustGameInfoDTO dto){
        Assert.notNull(dto.getId(),"必须提供id");
        Assert.notNull(dto.getCnName(),"必须提供中文名");
        Assert.notNull(dto.getEnName(),"必须提供英文名");
        Assert.notNull(dto.getImages(),"必须提供封面图片");
        Assert.notNull(dto.getWebsite(),"必须提供官网");
        Assert.notNull(dto.getDesc(),"必须提供描述");
        Assert.notNull(dto.getCompanyId(),"必须提供游戏开发公司id");
        Assert.notNull(dto.getAmount(),"必须提供价格");
        Assert.notNull(dto.getType(),"必须提供游戏类型");
        JustGameInfo justGameInfo = justGameInfoService.requireGetById(dto.getId(),"相关数据不存在");
        justGameInfo.setCnName(dto.getCnName());
        justGameInfo.setEnName(dto.getEnName());
        justGameInfo.setImages(dto.getImages());
        justGameInfo.setWebsite(dto.getWebsite());
        justGameInfo.setDesc(dto.getDesc());
        justGameInfo.setCompanyId(dto.getCompanyId());
        justGameInfo.setAmount(dto.getAmount());
        justGameInfo.setType(dto.getType());
        return Result.status(justGameInfoService.updateById(justGameInfo));
    }

    @PostMapping("/save")
    @RequiresAuthentication
    @ApiOperation(value = "新增 游戏信息表")
    public Result<?> save(@ApiParam("新增表单") @RequestBody JustGameInfoDTO dto){
        Assert.notNull(dto.getCnName(),"必须提供中文名");
        Assert.notNull(dto.getEnName(),"必须提供英文名");
        Assert.notNull(dto.getImages(),"必须提供封面图片");
        Assert.notNull(dto.getWebsite(),"必须提供官网");
        Assert.notNull(dto.getDesc(),"必须提供描述");
        Assert.notNull(dto.getCompanyId(),"必须提供游戏开发公司id");
        Assert.notNull(dto.getAmount(),"必须提供价格");
        Assert.notNull(dto.getType(),"必须提供游戏类型");
        JustGameInfo justGameInfo = new JustGameInfo();
        justGameInfo.setCnName(dto.getCnName());
        justGameInfo.setEnName(dto.getEnName());
        justGameInfo.setImages(dto.getImages());
        justGameInfo.setWebsite(dto.getWebsite());
        justGameInfo.setDesc(dto.getDesc());
        justGameInfo.setCompanyId(dto.getCompanyId());
        justGameInfo.setAmount(dto.getAmount());
        justGameInfo.setType(dto.getType());
        return Result.status(justGameInfoService.save(justGameInfo));
    }

    @DeleteMapping("/remove")
    @RequiresAuthentication
    @ApiOperation(value = "通过主键id删除 游戏信息表")
    public Result<?> remove(@ApiParam("主键id") @NotNull(message = "必须提供需要删除的数据") Long id){
        return Result.status(justGameInfoService.requireRemoveById(id, "抱歉，您的账号权限不支持删除此数据"));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<?> page(@ApiParam("查询表单") JustGameInfoQueryForm queryForm){
        IPage<JustGameInfoWrapper> page = justGameInfoService.selectJoinListPage(queryForm.buildPageQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",page.getRecords())
                    .put("rows",page.getTotal())
                .build());
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表查询")
    public Result<?> list(@ApiParam("查询表单") JustGameInfoQueryForm queryForm){
        List<JustGameInfoWrapper> list = justGameInfoService.selectJoinList(queryForm.buildListQuery());
        return Result.ok(MapBuilder.builder()
                    .put("data",list)
                .build());
    }
}